from flask import Blueprint,request,jsonify
bp_bumen = Blueprint("bumen",__name__)
from db import db1
from comm import get_data
from utils.myfile import myfile
import os

@bp_bumen.route("/deptM",methods=['POST','GET'])
def deptM():
    if request.method == 'POST':
        data = get_data()
        sql = "select id from dept where name="%(data['name'])
        res = db1.find(sql)
        if res:
            return jsonify({'code':10010,'msg':'已存在'})
        else:
            sql = "insert into dept(name) value('%s')"%(data['name'])
            db1.insert(sql)
            return jsonify({'code':200})
    else:
        sql = "select * from dept"
        res = db1.findall(sql)
        return jsonify({"code":200,'list':res})

@bp_bumen.route("/empM",methods=['GET','POST','DELETE'])
def empM():
    if request.method == 'POST':
        data = get_data()
        sql = "insert into emp values(0,'%s',%d,%.2f,%d)"%(data['name'],int(data['sex']),float(data['salary']),int(data['dept_id']))
        db1.insert(sql)
        sql1 = 'select total from dept where id=%d'%(int(data['dept_id']))
        res = db1.find(sql1)
        total = int(res['total'])+1
        sql2 = 'update dept set total=%d where id=%d'%(total,int(data['dept_id']))
        db1.insert(sql2)
        return jsonify({"code":200})
    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = "delete from set where id=%d"%(int(id))
        db1.insert(sql)
        return jsonify({'code':200})

# sql = "select emp.* from emp inner join dept on emp.dept_id=dept.id where dept.name='财务部'"
# sql = "select * from emp where dept_id=(select id from dept where name='财务部')"
# sql = "select * from emp where salary=(select max(salary) from emp)"
# sql = "select avg(salary),sex,group_concat(name) from emp group by sex"

@bp_bumen.route('/spotM',methods=['GET','POST'])
def spotM():
    if request.method == 'POST':
        data = request.form
        sql = "insert into spot(id,name,details,price,type_id) values(0,'%s','%s',%.2f,%d)"%(data['name'],data['details'],float(data['price']),int(data['type_id']))
        db1.insert(sql)
        print(sql)
        sql1 = "select id from spot where name='%s'"%(data['name'])
        print(sql1)
        rse = db1.find(sql1)
        print(rse)
        file = request.files['file']
        path = '../static/upload/'+str(rse['id'])
        myfile.file_mkdir(str(rse['id']))
        file.save(os.path.join(path,file.filename))
        sql2 = "update spot set pic='%s' where id=%d"%(path+file.filename,int(rse['id']))
        print(sql2)
        db1.insert(sql2)
        return jsonify({'code':200})
    else:
        sql = "select * from spot"
        rse = db1.find_all(sql)
        return jsonify({'code':200,'list':rse})

@bp_bumen.route('/spottype',methods=['GET','POST'])
def spottype():
    if request.method == 'POST':
        data = get_data()
        sql = "insert into spot_type values(0,'%s','%s')"%(data['name'],data['lei'])
        db1.insert(sql)
        return jsonify({'code':200})
    else:
        sql = "select * from spot_type"
        rse = db1.find_all(sql)
        return jsonify({'code':200,'list':rse})